package cn.imokay.juc.threadshutdown;

import java.util.concurrent.TimeUnit;

public class VolatileDemo {
    static volatile boolean stoped = false;

    public static void main(String[] args) {
        new Thread(()->{
            while (true) {
                if (stoped) {
                    System.out.println(Thread.currentThread().getName() + "\t---isStoped为ture,被协商终止---");
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "\t---hello,volatile---");
            }
        },"t1").start();

        try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace();}

        new Thread(()->{
                stoped = true;
        },"t2").start();
    }
}
